﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenNETCF.Desktop.Communication;

namespace Miris.HumanMilkAnalyser.Service.Shared
{
    public static class MiniLogHelper
    {
        /// <summary>
        /// Trying to connect to device, check the remote minilog file and sync it to local if needed.
        /// </summary>
        public static void SyncMiniLogFileFromDevice()
        {
            var miniLogFileName = (string)ConfigFileUtility.GetValue(ConfigurationConstant.MiniLogFileName);
            var localMiniLogFileDirectoryName = (string)ConfigFileUtility.GetValue(ConfigurationConstant.LocalMiniLogFileDirectoryName);
            var localMiniLogFileDirectoryFullName = Path.Combine(Environment.CurrentDirectory, localMiniLogFileDirectoryName);
            var localMiniLogFileFullName = Path.Combine(localMiniLogFileDirectoryFullName, miniLogFileName);
            var remoteMiniLogFileDirectoryName = (string)ConfigFileUtility.GetValue(ConfigurationConstant.RemoteMiniLogFileDirectoryName);
            var remoteMiniLogFileFullName = Path.Combine(remoteMiniLogFileDirectoryName, miniLogFileName);

            // Check if current minilog file directory exists
            if (!Directory.Exists(localMiniLogFileDirectoryFullName))
            {
                // Create local minilog file directory
                Directory.CreateDirectory(localMiniLogFileDirectoryFullName);
            }

            RAPI device = null;

            // If Windows Mobile Device Center is not installed, expection will occur.
            try
            {
                device = new RAPI();
            }
            catch
            {
                throw new Exception("当前系统未正确安装微软Windows Mobile设备中心，请联系设备提供商");
            }

            using (device)
            {
                // Check if device is connected
                if (device.DevicePresent)
                {
                    // Connect to device
                    device.Connect();

                    // Check if minilog file exists on device
                    if (!device.DeviceFileExists(remoteMiniLogFileFullName))
                    {
                        throw new ArgumentException("未找到设备数据文件，请联系设备提供商");
                    }

                    // Check if local minilog file does not exist or minilog file size is different between local and device
                    if (!File.Exists(localMiniLogFileFullName) || device.GetDeviceFileSize(remoteMiniLogFileFullName) != (new FileInfo(localMiniLogFileFullName)).Length)
                    {
                        // Copy the minilog file from device to local
                        device.CopyFileFromDevice(localMiniLogFileFullName, remoteMiniLogFileFullName, true);
                    }

                    device.Disconnect();
                }
                else
                {
                    throw new Exception("当前设备未连接");
                }
            }
        }
    }
}
